// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;
using System.Text;

namespace SnapObjects.Data.MySql
{
    public class MySqlGenerationHelper : SqlGenerationHelper, ISqlGenerationHelper
    {
        public MySqlGenerationHelper(IDataContextOptions contextOptions) : base(contextOptions)
        {

        }

        public override char ParameterIdentifier => '@';

        public override string BatchTerminator => "GO" + Environment.NewLine + Environment.NewLine;

        protected override string LeftDelimitIdentifier => "`";

        protected override string RightDelimitIdentifier => "`";

        public override string DelimitIdentifier(string identifier)
        {
            if (!String.IsNullOrWhiteSpace(identifier) &&
                identifier.StartsWith(this.LeftDelimitIdentifier) &&
                identifier.EndsWith(this.RightDelimitIdentifier))
            {
                return identifier;
            }

            return base.DelimitIdentifier(identifier);
        }

        public override void DelimitIdentifier(StringBuilder builder, string identifier)
        {
            if (!String.IsNullOrWhiteSpace(identifier) &&
                identifier.StartsWith(this.LeftDelimitIdentifier) &&
                identifier.EndsWith(this.RightDelimitIdentifier))
            {
                builder.Append(identifier);
            }
            else
            {
                base.DelimitIdentifier(builder, identifier);
            }
        }
    }
}
